//
//  YFSetUpViewController.m
//  SquashSpark
//
//  Created by Coollang on 2017/12/4.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFSetUpViewController.h"
#import "UserGroup.h"
#import "NSString+Extension.h"
#import "YFGeneralViewController.h"
#import "YFPersonDataVC.h"
#import "YFRacquetController.h"
#import "YFEquipmentCalibrationVC.h"
#import "YFPostTopicViewController.h"
#import "MyNavigationViewController.h"
#import "YFClubSettingVC.h"
#import "JCEditClubInformationVC.h"
#import "YFPersonDataVC.h"
#import "YFLoginViewController.h"
#import "MyNavigationViewController.h"
#import "AppDelegate+extension.h"
#import "YFAboutViewController.h"
#import "YFSearchController.h"
#import "WkWebView/WKWebviewController.h"

@interface YFSetUpViewController ()<YFSeletePhotoManagerDelegate,CAAnimationDelegate>
@property (nonatomic, strong) UITableView *tabelView;
@property (strong, nonatomic) YFSeletePhotoManager *seletePhotoManager;
@end

@implementation YFSetUpViewController

- (void)loadView {
    self.tabelView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.tabelView.backgroundColor = kSeparationLineColor;
    self.tabelView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    self.view = self.tabelView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kSeparationLineColor;
    [self loadDataItem];
    [self creatLogOutButton];
    self.tabelView.sectionFooterHeight = 10;
    
    [self regiesterNotifation];
    self.navigationItem.title = @"Settings";
    self.navigationItem.leftBarButtonItem = nil;
}

- (void)regiesterNotifation {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateReaquetType:) name:kUpdateRacquetTypeNoti object:nil];
}

// 更新球拍类型
- (void)updateReaquetType:(NSNotification *)noti {
    [self loadDataItem];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self loadDataItem];
    [self.tabelView reloadData];
}

- (void)loadDataItem {
    if (self.groups.count > 0) {
        [self.groups removeAllObjects];
    }
    __weak typeof(self) weakSelf = self;
    
    UserGroup *group0 = [[UserGroup alloc] init];
    UserArrowItem *item0_0 = [UserArrowItem itemWithIcon:nil andTitle:@"Personal Data" andTarget:nil];
    [item0_0 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        YFPersonDataVC *personVc = [[YFPersonDataVC alloc] init];
        [personVc setSaveUserInfoBlock:^(BOOL success) {
            
        }];
        [weakSelf.navigationController pushViewController:personVc animated:YES];
    }];
    
    UserArrowItem *item0_1 = [UserArrowItem itemWithIcon:nil andTitle:@"Club Setting" andTarget:[YFClubSettingVC class]];
    if ([[User getUseInfo] clubUserType] == ClubUserTypeVisitor) {
        [group0.items addObjectsFromArray:@[item0_0]];
    }else{
        [group0.items addObjectsFromArray:@[item0_0,item0_1]];
    }
    
    UserGroup *group1 = [[UserGroup alloc] init];
    
    UserArrowItem *item1_0 = [UserArrowItem itemWithTitle:@"My Spark Sensor" andSubTitle:nil];
    [item1_0 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        [weakSelf connectSensor];
    }];
    
    UserArrowItem *item1_1 = [UserArrowItem itemWithIcon:nil andTitle:@"Unit Of Measure" andTarget:[YFGeneralViewController class]];
    
    UserItem *item1_2 = [UserItem itemWithTitle:@"Racket Selection" andSubTitle:[SquashEnum stringWithRacquet:[User getUseInfo].racquetType]];
    [item1_2 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        YFRacquetController *racquetVc = [[YFRacquetController alloc] init];
        [weakSelf.navigationController pushViewController:racquetVc animated:YES];
    }];
    
    UserItem *item1_3 = [UserItem itemWithTitle:@"Equipment Calibration" andSubTitle:[User getUseInfo].equipmentCalibration ? @"Complete":@"Not set"];
    
    [item1_3 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        YFEquipmentCalibrationVC *equipmentVc = [[YFEquipmentCalibrationVC alloc] initWithNibName:NSStringFromClass([YFEquipmentCalibrationVC class]) bundle:nil];
        equipmentVc.title = itemT.title;
        [weakSelf.navigationController pushViewController:equipmentVc animated:YES];
    }];
    
    [group1.items addObjectsFromArray:@[item1_0,item1_1,item1_2,item1_3]];
    
    UserGroup *group2 = [[UserGroup alloc] init];
    UserArrowItem *item2_0 = [UserArrowItem itemWithIcon:nil andTitle:@"About" andTarget:nil];
    [item2_0 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        YFAboutViewController *aboutVc = [[YFAboutViewController alloc] initWithNibName:@"YFAboutViewController" bundle:nil];
        [weakSelf.navigationController pushViewController:aboutVc animated:YES];
    }];
    
    UserArrowItem *item2_1 = [UserArrowItem itemWithIcon:nil andTitle:@"User Feedback" andTarget:nil];
    [item2_1 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        [weakSelf userFeedBack];
    }];
    
    UserArrowItem *item2_2 = [UserArrowItem itemWithIcon:nil andTitle:@"Privacy" andTarget:nil];
    
    [item2_2 setBlock:^(UserItem *itemT, NSIndexPath *indexpath) {
        [weakSelf openPrvacyProtocol];
    }];
    
    UserItem *item2_3 = [UserItem itemWithIcon:nil andTitle:[NSString stringWithFormat:@"Version %@",[NSString appCurrentVersion]]];
    [group2.items addObjectsFromArray:@[item2_0,item2_1,item2_2,item2_3]];
    
    [self.groups addObjectsFromArray:@[group0,group1,group2]];
}

- (void)connectSensor{
    [YFSensorManager openSensorControllrWithVc:self isNecessaryOpenSensorVc:YES];
}

- (void)userFeedBack {
    if (self.seletePhotoManager != nil) {
        self.seletePhotoManager = nil;
    }
    YFSeletePhotoManager *seleteManager = [[YFSeletePhotoManager alloc] init];
    self.seletePhotoManager = seleteManager;
    seleteManager.delegate = self;
    YFPostTopicViewController *topicVc = [[YFPostTopicViewController alloc] init];
    topicVc.seletePhotoManager = self.seletePhotoManager;
    topicVc.type = PostTopicTypeFeedBack;
    MyNavigationViewController *navVc = [[MyNavigationViewController alloc] initWithRootViewController:topicVc];
    topicVc.seletePhotoManager.delegate = topicVc;
    [self presentViewController:navVc animated:YES completion:nil];
}

- (void)openPrvacyProtocol {
    WKWebviewController *webVC = [[WKWebviewController alloc] init];
    webVC.urlString = kSettingPrivacyUrl;
    webVC.progressColor = kYellowGreenColor;
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)creatLogOutButton {
    UIButton *logoutBtn = [UIButton creatButtonTitle:@"Log out" bgColor:kBgThemeColor textFont:kOpenSansFont(OpenSansRegular, 16) textColor:kTRedEEText];
    logoutBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    logoutBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 16, 0, 0);
    logoutBtn.frame = CGRectMake(0, 0, kYFScreenWidth, 50);
    [logoutBtn addTarget:self action:@selector(logOutBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.tabelView.tableFooterView = logoutBtn;
}

- (void)logOutBtnClick {
    FUIAlertView *alert = [FUIAlertView alertWithTitle:@"Prompt" message:@"Confirm to sign out?" cancelTitle:@"No" confirmTitle:@"Yes"];
    [alert show];
    [alert setOnOkAction:^{
        [AppDelegate logoutWithShowLoginVC:YES];
    }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UserCenterCell *cell = [UserCenterCell settingViewCellTableView:tableView withStyle:UITableViewCellStyleValue1];
    
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    cell.item = item;
    
    if (indexPath.row ==  group.items.count - 1) {
        [cell showSeparationBottomLine:NO];
    }else {
        [cell showSeparationBottomLine:YES];
    }
    cell.separationBottomLineHeight = 1;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.000001;
    }
    return 0.00001;
}
- (void)seletePhotoDidFinishPickingPhotos:(NSArray<UIImage *> *)photos assets:(NSArray *)assets isOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    
}

@end
